$(document).ready(function () {
    var buscadorMapa = {
        geocoder: new google.maps.Geocoder(),
        southWest: new google.maps.LatLng(-54.952386,-73.081055),
        northEast: new google.maps.LatLng(-22.105999,-53.613281),
        latLngBounds: new google.maps.LatLngBounds(new google.maps.LatLng(-54.952386,-73.081055),new google.maps.LatLng(-22.105999,-53.613281)),

        bind: function () {
            var self = this;
            $('#mapa-search-form button, #cooperativas-search-form button').click(function(e){
                e.preventDefault()
                if ($("#mapa-search-input, #cooperativas-search-input").val()) {
                    self.buscar();
                }
            });
            $('#mapa-search-input, #cooperativas-search-input').bind('keypress', function(e) {
                if(e.keyCode==13){
                    e.preventDefault()
                    if ($("#mapa-search-input, #cooperativas-search-input").val()) {
                        self.buscar();
                    }
                }
            });

            $('#mapa-search-input, #cooperativas-search-input').autocomplete({
                source: function(request, response){
                    buscadorMapa.geocoder.geocode({
                        address: request.term + ' Argentina',
                        region: 'AR',
                        bounds: buscadorMapa.latLngBounds
                    }, function(results, status){
                        response($.map(results, function(item){
                            return {
                                label: item.formatted_address,
                                value: item.formatted_address,
                                latitude: item.geometry.location.lat(),
                                longitude: item.geometry.location.lng()
                            }
                        }));
                    })
                },
                select: function(event, ui){
                    var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
                    mapaController.map.defs.map.setCenter(location);
                    mapaController.map.defs.map.setZoom(13);
                    /*marker = new google.maps.Marker({
                        map: mapaController.map.defs.map,
                        position: location
                    });*/
                }
            });
        },
        buscar: function () {
            var direccion = $('#mapa-search-input, #cooperativas-search-input').val();
            $('#mapa-search-input, #cooperativas-search-input').val('');
            $('#mapa-search-input, #cooperativas-search-input').autocomplete('close');
            this.processGeocoding(direccion, this.centarMapa);
        },

        processGeocoding: function (location, callback) {
            var self = this;
            var request = {
                language: 'es',
                region: 'AR',
                address: location + ' Argentina'
            }
            this.geocoder.geocode(request, function(results, status) {
                if(status == google.maps.GeocoderStatus.OK) {
                    self.centarMapa (results);
                    return results;
                }
                return status;
            });
        },

        centarMapa: function (geocodes) {
            mapaController.map.defs.map.setZoom(13);
            mapaController.map.defs.map.setCenter(geocodes[0].geometry.location);
            /*marker = new google.maps.Marker({
                map: mapaController.map.defs.map,
                position: geocodes[0].geometry.location
            });*/
        },

        init: function () {
            this.bind();
        }
    }
    buscadorMapa.init()
})